<?php
/**
 * 我的
 *
 * User: kaikaic
 * Date: 2018/12/5
 * Time: 20:42
 */

namespace app\api\controller;

use app\admin\service\CityService;
use app\admin\service\TagsService;
use app\api\service\OrganizationService;
use app\psy\controller\Account;
use app\psy\service\AccountService;
use controller\BasicApi;
use think\Db;

class Organization extends BasicApi {

    public function index() {
        //TODO
        $size = 5;
        $page = intval($this->request->param("page",1));
        $start = ($page-1)*$size;
        $res['status'] = 1;
        $city = intval($this->request->param("areaid",0));
        if($city){
            $res['city_id'] = $city;
        }
        $price = intval($this->request->param("priceid",0));
        if($price){
            $res['price'] = $price;
        }
        $category_id = intval($this->request->param("type"));
        if($category_id){
            $res['category_id'] = array('in',$category_id);
        }

        $limit = "$start,$size";
        $records = OrganizationService::getInstance()->selectlimit($res, 0,'',$limit);
        foreach ($records as $key =>&$item){
            $city = CityService::getInstance()->single(array('area_id'=>$item['city_id']));
            $item['city'] = $city['area_name'];
            $consultType = $this->readCategoryConfig("psy_price");
            $organizationType = $this->readCategoryConfig("organization");


            $item['pricename'] = $consultType[$item['price']];
            $item['category_name'] = $organizationType[$item['category_id']];
        }
        $this->success('获取成功',$records);
    }
    public function getoption(){
        $res['priceType'] = $this->readCategoryConfig("psy_price");
        $res['organizationType'] = $this->readCategoryConfig("organization");
        $res['area'] = CityService::getInstance()->select(array('area_parent_id'=>0));
        $this->success('获取成功',$res);
    }
    public function getarealist(){
        $province = intval($this->request->param("province"),0);
        $area = CityService::getInstance()->select(array('area_parent_id'=>$province));
        $this->success('获取成功',$area);
    }
    /**
     * 组织信息
     *
     * psy.game.com/api/me/info?uid=274&nick_name=花儿多多K&gender=2&province_id=18&city_id=275&intro=aaaaaaaaaaaaaaaa
     */
    public function info() {
        $id = intval($this->request->param("id"));
        $account_id = intval($this->request->param("account_id",0));
        // 是否是咨询师
        $record = OrganizationService::getInstance()->single(array('id'=>$id));
        if (!$record) {
            $this->error("没有这个机构");
        }
        // '1'=>'待审核', '2'=>'已通过', '3'=>'已拒绝', '4'=>'已禁用'
//        if ($record['status'] != 1) {
////            $this->error("没有这个机构", 401);
////        }
        $record['isfollow'] = 0;
        if($account_id){
            $res = Db::name('psy_organization')
                -> where('account_id',$account_id)
                ->where('type','org')
                ->where('target_id',$id)->select();
            if($res){
                $record['isfollow'] = 1;
            }
        }
        $city = CityService::getInstance()->single(array('area_id'=>$record['city_id']));
        $record['city'] = $city['area_name'];
        $province = CityService::getInstance()->single(array('area_id'=>$record['province_id']));
        $record['province'] = $city['area_name'];
        $consultType = $this->readCategoryConfig("psy_price");
        $organizationType = $this->readCategoryConfig("organization");

        $record['pricename'] = $consultType[$record['price']];
        $record['category_name'] = $organizationType[$record['category_id']];

        $this->success('获取成功',$record);
    }


    public function orgtags(){
        $res['organization']  =   Db::name('system_category')->where('pid',13)->select();
        $res['psy_price']  =   Db::name('system_category')->where('pid',20)->select();
        $this->success('获取成功',$res);
    }
    public function orginfo(){
        $data = $this->request->param();
        if (empty($data['id'])) {
            $acccount_id = $data['accountid'];

            unset($data['accountid']);
            $data['sort'] = OrganizationService::getInstance()->getMaxSort() + 1;
            $data['status'] = 0;
            $data['create_time'] = time();
            $result = OrganizationService::getInstance()->insert($data);
            $datares['org_id'] = $result;
            $res = Db::name("account")->where('id',$acccount_id)->update($datares);
            if($res && $result){
                $this->success('申请成功',$result);
            }

        } else {
            $id = intval($data['id']);
            unset($data['accountid']);
            $result = OrganizationService::getInstance()->update($data, $id);
            if( $result>=0){
                $this->success('修改成功',$result);
            }
        }
    }

}